package math.leetcode_169;

public class MooreSolution {
    /**
     * 摩尔投票法
     */
    public int majorityElement(int[] nums) {
        int record = nums[0];
        int count = 0;
        for (int num : nums) {
            if (num == record) {
                count++;
            } else {
                count--;
            }
            if (count == 0) {
                record = num;
                count++;
            }
        }
        return record;
    }
}
